package com.example.superoa.model.service;

import java.math.BigDecimal;
import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.aspectj.apache.bcel.generic.RET;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.superoa.common.Shijian;
import com.example.superoa.model.dao.KqOverworkDao;
import com.example.superoa.model.dao.KqOverworkXiangDao;
import com.example.superoa.model.dao.StaffDao;
import com.example.superoa.model.mappers.BanCiMapper;
import com.example.superoa.model.mappers.OvertimeMapper;
import com.example.superoa.model.mappers.OvertimeXiangMapper;
import com.example.superoa.model.mappers.OvertimestatiMapper;
import com.example.superoa.model.mappers.RecessMapper;
import com.example.superoa.model.pojos.KqBanci;
import com.example.superoa.model.pojos.KqOverwork;
import com.example.superoa.model.pojos.KqOverworkXiang;

@Service
@Transactional(rollbackFor=Exception.class)
public class OvertimeService {
	@Autowired
	private OvertimeMapper map;
	
	@Autowired 
	private OvertimeXiangMapper x_map;
	
	@Autowired
	private OvertimestatiMapper s_map;
	
	@Autowired
	private AttendService a_ser;
	
	@Autowired
	private KqOverworkDao o_dao;
	
	@Autowired
	private BanCiMapper b_map;
	
	@Autowired
	private KqOverworkXiangDao ox_dao;
	
	@Autowired
	private RecessMapper r_map;
	
	@Autowired 
	private StaffDao s_dao;
	
	Shijian sj=new Shijian();
	
	public JSONArray selectAll() {
		return map.overtimeSelectAll();
	}
	
	public JSONObject selectId(int id) {
		return map.selectOvertimeId(id);
	}
	public String pan(Integer type,Integer tian,Date date) {
		String i="";
		SimpleDateFormat sdf=new SimpleDateFormat("HH:mm:ss");
		Date shiyi = null;
		try {
			shiyi=sdf.parse("23:00:01");
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(shiyi);
		if(type==0) {
			JSONObject banci=b_map.selectqiyong();
			Date time=sj.getDaiTime(banci.getDate("b_tui"),tian);
				if(panduan(date,1)) {
					i="您所选的日期不为工作日";
				}
				if(panduan(date,2)) {
					i="您所选的日期不为工作日";
				}
				if(sj.getPanduanShi1(time,shiyi)>=0){
					i="工作日加班不能超过23点";
				}
		}else if(type==1){
			if(tian==1) {
				if(panduan(date,1)) {
					i="您所选的日期不为休息日";
				}
				if(!sj.panduanSun(date)) {
					i="您所选的日期不为休息日";
				}
			}else {
				for (Date day : sj.getDays(1, date, sj.getDaiDate(date, tian))) {
					if(panduan(day,1)) {
						i="您所选的日期不为休息日";
					}
					if(!sj.panduanSun(date)) {
						i="您所选的日期不为休息日";
					}
				}
			}
		}else {
			if(tian==1) {
				if(panduan(date,2)) {
					i="您所选的日期不为节假日";
				}
			}else {
				for (Date day : sj.getDays(1, date, sj.getDaiDate(date, tian))) {
					if(panduan(day,2)) {
						i="您所选的日期不为节假日";
					}
				}
			}
		}
		return i;
	}
	public boolean panduan(Date date,Integer type) {
		boolean i;
		JSONObject jia=r_map.selectAllRess(date);
		if(jia!=null) {
			if(jia.getInteger("RE_CLASS")==type) {
				i=true;
			}else {
				i=false;
			}
		}else {
			i=false;
		}
		return i;
	}
	public BigDecimal selectOvertim(int year,int month,int staff_id,int type){
		return map.selectOvertim(year, month,staff_id,type);
	}
	public JSONArray selectXiang(int id){
		return x_map.selectXiang(id);
	}
	public JSONObject selectStatical(Integer year,Integer month,Integer staff_id) {
		return s_map.selectOverstatiId(year, month, staff_id);
	}
	public void addovertime(KqOverwork over,String oWhy,int type,int tian,int staff_id) {
		Date jie;
		BigDecimal time;
		Integer id=map.selectOverid();
		if(id==null) {
			id=1;
		}else {
			id++;
		}
		if(type==0) {
			jie=over.getOKai();
		}else {
			jie=sj.getDaiDate(over.getOKai(), tian-1);
		}
		time=new BigDecimal(tian);
		over.setStaff(s_dao.findById(staff_id).get());
		over.setOShen(0);
		over.setODate(new Date(sj.getNetworkTime()));
		over.setOJie(jie);
		over.setOTime(time);
		over.setOId(id);
		over.setoWhy(oWhy);
		over.setOXshen(a_ser.selectDeptStaff(staff_id).toString());
		o_dao.save(over);
		addoverXiang(id,time,type);
	}
	public void addoverXiang(Integer id,BigDecimal time,Integer type) {
		KqOverworkXiang kq=new KqOverworkXiang();
		kq.setKqOverwork(o_dao.findById(id).get());
		kq.setOxTime(time);
		kq.setOxType(type);
		ox_dao.save(kq);
	}
	public void shen(int id,int shen,String str) {
		KqOverwork pojo=o_dao.findById(id).get();
		if(shen==2) {
			pojo.setOBou(str);
		}
		pojo.setoSdate(new Date(sj.getNetworkTime()));
		pojo.setOShen(shen);
		o_dao.save(pojo);
	}
	public JSONObject selectbetween(int id,Date date) {
		return map.selectbetween(id, date);
	}
	
}
